﻿using Axiom.Hermetica.WPF.Models;
using Axiom.Hermetica.WPF.ViewModels;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Navigation;

namespace Axiom.Hermetica.WPF.Views
{
	public partial class LibraryPage : Page
	{
		public ViewModel Model { get { return DataContext as ViewModel; } }

		private Predicate<object> FilterByCategory { get; set; }
		private Predicate<object> FilterB { get; set; }

		private ICollectionView CollectionView
		{
			get
			{
				return CollectionViewSource.GetDefaultView(BookListBox.ItemsSource);
			}
		}

		private Predicate<object> Filter
		{
			set
			{
				if (FilterByCategory != null && !object.ReferenceEquals(FilterByCategory,value))
				{
					CollectionView.Filter = new Predicate<object>(a => { return FilterByCategory(a) && value(a); });
				}
				else
				{
					CollectionView.Filter = value;
				}
				Model.Filter = CollectionView.Filter;
			}
		}

		public LibraryPage()
		{
			InitializeComponent();
			Model.Dispatcher = Dispatcher;
		}

		private void OnCategoryChanged(object sender, SelectionChangedEventArgs e)
		{
			if (BookListBox == null)
			{
				return;
			}

			if (Model.Filter != null)
			{
				Filter = Model.Filter;
			}

			if (Model.SelectedCategory == "All")
			{
				FilterByCategory = null;
				Filter = null;
			}
			else
			{
				FilterByCategory = new Predicate<object>(a =>
				{
					return (a as Book).Category.Equals(Model.SelectedCategory);
				});
				Filter = FilterByCategory;
			}
		}

		private void OnDrop(object sender, DragEventArgs e)
		{
			string[] items = e.Data.GetData(DataFormats.FileDrop, true) as string[];

			Model.AddItems(items);
		}

		private void OnEditSelectedItem(object sender, RoutedEventArgs e)
		{
			if (BookListBox.SelectedItem == null)
			{
				return;
			}

			if (Model.SelectedItems.Count > 1)
			{
				Model.SelectedBookPath = Model.SelectedItem.Path;
				var uri = new Uri(@"\Views\Windows\MultipleEditorPage.xaml", UriKind.Relative);
				NavigationService.Navigate(uri);
			}
			else if (Model.SelectedItems.Count == 1)
			{
				Model.SelectedBookPath = Model.SelectedItem.Path;
				var uri = new Uri(@"\Views\Windows\SingleEditorPage.xaml", UriKind.Relative);
				NavigationService.Navigate(uri);
			}
		}

		private void OnGetThumbs(object sender, RoutedEventArgs e)
		{
			Model.GetThumbsForSelected();
		}

		private void OnGetThumbsForSelected(object sender, RoutedEventArgs e)
		{
			Model.GetThumbsForSelected();
		}

		private void OnItemDoubleClick(object sender, MouseButtonEventArgs e)
		{
			OnOpen(null, null);
		}

		private void OnKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
		{
			if (e.Key == System.Windows.Input.Key.Delete)
			{
				Model.DeleteSelectedItems();
			}
		}

		private void OnLoaded(object sender, RoutedEventArgs e)
		{
			FilterByCategory = new Predicate<object>(a => { return (a as Book).Image != null; });
			Filter = FilterByCategory;
		}

		private void OnOpen(object sender, RoutedEventArgs e)
		{
			Model.OpenSelected();
		}

		private void OnSave(object sender, RoutedEventArgs e)
		{
			Model.Save();
		}

		private void OnSearchTermKeyDown(object sender, KeyEventArgs e)
		{
			var B = new Predicate<object>(a =>
			{
				var p1 = string.IsNullOrWhiteSpace(SearchTerm.Text);

				var p2 = (a as Book).Name.IndexOf(SearchTerm.Text, StringComparison.CurrentCultureIgnoreCase) > -1;

				return p1 || p2;
			});

			Filter = B;
		}

		private void OnSearchTermTextBoxGotFocus(object sender, RoutedEventArgs e)
		{
			if (Model.SearchTerm.Equals("SEARCH"))
			{
				Model.SearchTerm = string.Empty;
			}
		}

		private void OnSearchTermTextBoxLostFocus(object sender, RoutedEventArgs e)
		{
			if (string.IsNullOrWhiteSpace(Model.SearchTerm))
			{
				Model.SearchTerm = "SEARCH";
			}
		}

		private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			Model.UpdateSelectedSet(e.AddedItems, e.RemovedItems);
		}

		private void OnOpenInExplorer(object sender, RoutedEventArgs e)
		{
			Model.OpenInExplorer();
		}
	}
}